import { BaseTable } from '../db/table'
import { Result } from './index'
import moment from 'moment'

export enum Filetype {
  file = 'file',
  dir = 'dir'
}

export interface IFiles {
  name: string
  type: Filetype
  pid?: number
  article_id?: number | string
  create_time?: number | string | Date
  update_time?: number | string | Date
  version?: number
  collect?: boolean
  icon?: string
}

/**
 * 文件对象
 *
 * 不计算id， 让其自增
 */
export class FilesModel implements IFiles {
  public name: string
  public type: Filetype
  public pid: number = 0
  public article_id: number | string = ''
  public create_time?: number | string | Date
  public update_time?: number | string | Date
  public version?: number = 1
  public collect?: boolean = false
  public icon?: string

  constructor(opt: IFiles) {
    this.name = opt.name
    this.type = opt.type
    this.pid = opt.pid || 0
    this.article_id = opt.article_id || ''
    this.create_time = opt.create_time || moment().format()
    this.update_time = opt.update_time || moment().format()
    this.version = opt.version || 1
    this.collect = opt.collect || false
    this.icon =
      opt.icon ||
      (opt.type === Filetype.dir ? 'icon-wenjianjiafolder80' : 'icon-code')
  }
}

export class UpdateFileModel extends FilesModel {
  public id: number
  constructor(opt: IFiles, id: number) {
    super(opt)
    this.id = id
  }
}

export class FilesService extends BaseTable {
  public declare db: IDBDatabase
  public table: string

  constructor(db: IDBDatabase, table: string) {
    super()
    this.db = db
    this.table = table
  }

  /** 添加数据 */
  async push(data: IFiles) {
    let file = new FilesModel(data)
    let res: Result = new Result()
    try {
      res.data = await this.add(this.table, file)
      res.code = 1
    } catch (err) {
      res.code = 0
      res.msg = err.message
    }

    return res
  }

  async findById(id: number) {
    let res: Result = new Result()
    try {
      res.data = await this.getById(this.table, id)
      res.code = 1
    } catch (err) {
      res.code = 0
      res.msg = err.message
    }

    return res
  }

  async findByIndex(key: string) {
    let res: Result = new Result()
    try {
      res.data = await this.getByIndex(this.table, 'name', key)
      res.code = 1
    } catch (err) {
      res.code = 0
      res.msg = err.message
    }

    return res
  }

  async findAll() {
    let res: Result = new Result()
    try {
      res.data = await this.getAll(this.table)
      res.code = 1
    } catch (err) {
      res.code = 0
      res.msg = err.message
    }

    return res
  }

  async updata(data: FilesModel, id: number) {
    let res: Result = new Result()
    let file = new FilesModel(data)

    Object.assign(file, {
      version: file.version! + 1,
      id,
      update_time: moment().format()
    })
    try {
      res.data = await this.put(this.table, file)
      res.code = 1
    } catch (err) {
      res.code = 0
      res.msg = err.message
    }

    return res
  }

  async delete(id: number) {
    let res: Result = new Result()
    try {
      res.data = await this.del(this.table, id)
      res.code = 1
    } catch (err) {
      res.code = 0
      res.msg = err.message
    }

    return res
  }
}
